<?php
/**
 * Created by PhpStorm.
 * User: jswei
 * Date: 2018/5/3
 * Time: 14:06
 */
namespace app\first\model;

use think\Db;
use think\Model;

class Comments extends Model{
    protected $autoWriteTimestamp = true;

    /**
     * @param int $id
     * @param string $content
     * @param int $uid
     * @param int $flow_id
     * @param string $out
     * @return bool
     */
    public function comments($id=0,$content='',$uid=0,$flow_id=0,&$out=''){
        $info = (new Article())->find($id);
        if(!$info){
            $out = '内容已不存在';
            return false;
        }
        $data = [
            'aid'=>$id,
            'content'=>$content,
            'uid'=>$uid,
            'status'=>2,
            'create_time'=>time(),
        ];
        $valid = new \app\first\validate\Comments();
        if(!$valid->check($data)){
            $out = $valid->getError();
            return false;
        }
        Db::startTrans();
        $id = $this->insertGetId($data);
        if($flow_id){
            $_info = $this->find($flow_id);
            $_info->flow_id = $_info['flow_id'] ? $_info['flow_id'].','.$id : $id;
            $result = $_info->save();
        }else{
            $result = true;
        }
        if($result && $id){
            Db::commit();
            return true;
        }else{
            Db::rollback();
            return false;
        }
    }

    /**
     * @param array $where
     * @param string $order
     * @param int $limit
     * @param string $filed
     * @return \think\Paginator
     * @throws \think\exception\DbException
     */
    public function getList($where=[],$order='create_time desc',$limit=10,$filed='*'){
        $list = $this
            ->field($filed)
            ->with(['user'=>function($query){
                $query->field('id,account,nickname');
            }])
            ->where($where)
            ->order($order)
            ->paginate($limit);
        foreach ($list as &$v){
            if($v['flow_id']){
                $v['flow'] = $this->field($filed)->with(['user'=>function($query){
                    $query->field('id,account,nickname');
                }])->select();
            }else{
                $v['flow'] = [];
            }
        }
        return $list;
    }

    public function user(){
        return $this->hasOne('member','id','mid');
    }

    /**
     * 收藏
     * @param int $mid
     * @param int $uid
     * @param int $type
     * @param string $out
     * @return bool
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function subscribe($mid=0,$uid=0,$type=1,&$out=''){
        if(!$mid || !$uid){
            $out = '缺少参数';
            return false;
        }
        $info = $this
            ->where('mid','eq',$mid)
            ->where('rid','eq',$uid)
            ->find();
        if($type==1){
            if($info){
                $out = '你已经粉丝了';
                return false;
            }
            if(!$this->save([
                'mid'=>$mid,
                'rid'=>$uid
            ])){
                $out = '关注失败';
                return false;
            }
            return true;
        }else{
            if(!$info){
                $out = '你不是粉丝';
                return false;
            }
            if(!$info->delete()){
                $out = '取消失败';
                return false;
            }
            return true;
        }
    }
}